﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class FormSupplyIntegratedBuild : System.Windows.Forms.Form
	{
		public FormSupplyIntegratedBuild(Document d, double dEq)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.doc = d;
			this.Init();
			this.levelList = this.GetAllLevel();
			base.CenterToParent();
			this.floorUserList.Add(new IntegratedBuildUser(this.levelList[0], this.levelList[this.levelList.Count - 1], "办公楼、商场", dEq));
			this.UpdataListView();
		}

		public FormSupplyIntegratedBuild(Document d, List<IntegratedBuildUser> floorList)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = d;
			this.Init();
			this.levelList = this.GetAllLevel();
			base.CenterToParent();
			this.floorUserList = floorList;
			this.UpdataListView();
		}

		private List<Level> GetAllLevel()
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(this.doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).ToList<Element>())
			{
				Level level = element as Level;
				if (level != null)
				{
					list.Add(level);
				}
			}
			return (from ll in list
			orderby ll.Elevation
			select ll).ToList<Level>();
		}

		private void ModifyFloorUp(int nIndex, Level lvT)
		{
			if (nIndex < 0 || nIndex >= this.floorUserList.Count)
			{
				return;
			}
			this.floorUserList[nIndex].leFrom = lvT;
			List<IntegratedBuildUser> list = new List<IntegratedBuildUser>();
			for (int i = nIndex - 1; i >= 0; i--)
			{
				list.Insert(0, this.floorUserList[i]);
				this.floorUserList.Remove(this.floorUserList[i]);
			}
			this.DelAndAddFloorsUp(lvT, ref list);
			this.floorUserList.InsertRange(0, list);
			this.UpdataListView();
		}

		private void DelAndAddFloorsUp(Level lvS, ref List<IntegratedBuildUser> floorTemp)
		{
			int num = this.levelList.IndexOf(lvS);
			if (floorTemp.Count == 0 && num <= this.levelList.Count - 1)
			{
				floorTemp.Add(new IntegratedBuildUser(this.levelList[0], this.levelList[num - 1], this.szUserName, 0.0));
				return;
			}
			for (int i = floorTemp.Count - 1; i >= 0; i--)
			{
				if (num - this.levelList.IndexOf(floorTemp[i].leTo) > 1)
				{
					floorTemp.Add(new IntegratedBuildUser(this.levelList[this.levelList.IndexOf(floorTemp[i].leTo) + 1], this.levelList[num - 1], this.szUserName, 0.0));
					return;
				}
				if (num - this.levelList.IndexOf(floorTemp[i].leTo) == 1)
				{
					return;
				}
				if (num - this.levelList.IndexOf(floorTemp[i].leTo) <= 0 && num - this.levelList.IndexOf(floorTemp[i].leFrom) > 0)
				{
					floorTemp[i].leTo = this.levelList[num - 1];
					return;
				}
				if (num - this.levelList.IndexOf(floorTemp[i].leFrom) <= 0)
				{
					floorTemp.RemoveAt(i);
				}
			}
		}

		private void ModifyFloorDown(int nIndex, Level lvT)
		{
			if (nIndex < 0 || nIndex >= this.floorUserList.Count)
			{
				return;
			}
			this.floorUserList[nIndex].leTo = lvT;
			List<IntegratedBuildUser> list = new List<IntegratedBuildUser>();
			for (int i = this.floorUserList.Count - 1; i >= nIndex + 1; i--)
			{
				list.Insert(0, this.floorUserList[i]);
				this.floorUserList.Remove(this.floorUserList[i]);
			}
			this.DelAndAddFloorsDown(lvT, ref list);
			this.floorUserList.AddRange(list);
			this.UpdataListView();
		}

		private void DelAndAddFloorsDown(Level lvS, ref List<IntegratedBuildUser> floorTemp)
		{
			int num = this.levelList.IndexOf(lvS);
			if (floorTemp.Count == 0 && num < this.levelList.Count - 1)
			{
				floorTemp.Add(new IntegratedBuildUser(this.levelList[num + 1], this.levelList[this.levelList.Count - 1], "办公楼、商场", 40.0));
				return;
			}
			for (int i = 0; i < floorTemp.Count; i++)
			{
				if (this.levelList.IndexOf(floorTemp[i].leFrom) - num > 1)
				{
					floorTemp.Insert(0, new IntegratedBuildUser(this.levelList[num + 1], this.levelList[this.levelList.IndexOf(floorTemp[i].leFrom) - 1], "书店", 40.0));
					return;
				}
				if (this.levelList.IndexOf(floorTemp[i].leFrom) - num == 1)
				{
					return;
				}
				if (this.levelList.IndexOf(floorTemp[i].leTo) - num > 0)
				{
					floorTemp[i].leFrom = lvS;
					return;
				}
				if (this.levelList.IndexOf(floorTemp[i].leTo) - num <= 0)
				{
					floorTemp.RemoveAt(i);
					i--;
				}
			}
		}

		private void Init()
		{
			this.listViewFloor.Clear();
			this.listViewFloor.Columns.Add("", 0);
			this.listViewFloor.Columns.Add("起始楼层", 80, HorizontalAlignment.Left);
			this.listViewFloor.Columns.Add("终止楼层", 80, HorizontalAlignment.Left);
			this.listViewFloor.Columns.Add("建筑用途", 150, HorizontalAlignment.Left);
			this.listViewFloor.Columns.Add("当量总值", 80, HorizontalAlignment.Left);
			this.cmbPipeDN.SelectedIndexChanged += this.cmbSelectedIndexChanged;
			this.cmbPipeDN.LostFocus += this.cmbLostFocus;
			this.listViewFloor.Controls.Add(this.cmbPipeDN);
			this.cmbPipeDN.Visible = false;
			this.cmbPipeDN.DropDownStyle = ComboBoxStyle.DropDownList;
            this.listViewFloor.Controls.AddRange(new System.Windows.Forms.Control[]
			{
				this.editBox
			});
			this.editBox.LostFocus += this.editBox_LostFocus;
			this.editBox.BorderStyle = BorderStyle.Fixed3D;
			this.editBox.Visible = false;
		}

		private void UpdataListView()
		{
			this.listViewFloor.Items.Clear();
			for (int i = 0; i < this.floorUserList.Count; i++)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.Tag = i;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = this.floorUserList[i].leFrom.Name;
				listViewSubItem.Tag = 1;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = this.floorUserList[i].leTo.Name;
				listViewSubItem.Tag = 2;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = this.floorUserList[i].szUser;
				listViewSubItem.Tag = 3;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = this.floorUserList[i].dEquivalent.ToString();
				listViewSubItem.Tag = 4;
				listViewItem.SubItems.Add(listViewSubItem);
				this.listViewFloor.Items.Add(listViewItem);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void listViewFloor_MouseClick(object sender, MouseEventArgs e)
		{
			this.ProcessCombobox(e);
		}

		private void listViewFloor_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.ProcessCombobox(e);
		}

		private void ProcessCombobox(MouseEventArgs e)
		{
			this.bIsAutoChanel = false;
			ListViewHitTestInfo listViewHitTestInfo = this.listViewFloor.HitTest(e.X, e.Y);
			this.selectedItem = listViewHitTestInfo.Item;
			this.selectedSubItem = listViewHitTestInfo.SubItem;
			if (this.selectedSubItem == null || this.selectedSubItem.Tag == null)
			{
				return;
			}
			if ((int)this.selectedSubItem.Tag == 1 || (int)this.selectedSubItem.Tag == 2 || (int)this.selectedSubItem.Tag == 3)
			{
				this.szUserName = this.selectedItem.SubItems[3].Text;
				this.InitComb((int)this.selectedItem.Tag, (int)this.selectedSubItem.Tag);
				this.cmbPipeDN.Text = this.selectedSubItem.Text;
				this.cmbPipeDN.Left = this.selectedSubItem.Bounds.Left;
				this.cmbPipeDN.Top = this.selectedSubItem.Bounds.Top;
				this.cmbPipeDN.Width = this.selectedSubItem.Bounds.Width;
				this.cmbPipeDN.Height = this.selectedSubItem.Bounds.Height;
				this.bIsAutoChanel = true;
				this.cmbPipeDN.Visible = true;
				return;
			}
			if ((int)this.selectedSubItem.Tag == 4)
			{
				this.editBox.Left = this.selectedSubItem.Bounds.Left;
				this.editBox.Top = this.selectedSubItem.Bounds.Top;
				this.editBox.Width = this.selectedSubItem.Bounds.Width;
				this.editBox.Height = this.selectedSubItem.Bounds.Height;
				this.editBox.Text = this.selectedSubItem.Text;
				this.bIsAutoChanel = true;
				this.editBox.Visible = true;
				this.editBox.SelectAll();
				this.editBox.Focus();
				return;
			}
			this.bIsAutoChanel = false;
			this.selectedItem = null;
			this.selectedSubItem = null;
			this.cmbPipeDN.Visible = false;
			this.editBox.Visible = false;
		}

		private void cmbSelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.selectedItem != null && this.selectedSubItem != null && this.bIsAutoChanel)
			{
				this.selectedSubItem.Text = ((ComboBox)sender).Text;
				if ((int)this.selectedSubItem.Tag == 1)
				{
					using (List<Level>.Enumerator enumerator = this.levelList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Level level = enumerator.Current;
							if (((ComboBox)sender).Text == level.Name)
							{
								int nIndex = (int)this.selectedItem.Tag;
								this.ModifyFloorUp(nIndex, level);
								break;
							}
						}
						goto IL_180;
					}
				}
				if ((int)this.selectedSubItem.Tag == 2)
				{
					using (List<Level>.Enumerator enumerator = this.levelList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Level level2 = enumerator.Current;
							if (((ComboBox)sender).Text == level2.Name)
							{
								int nIndex2 = (int)this.selectedItem.Tag;
								this.ModifyFloorDown(nIndex2, level2);
								break;
							}
						}
						goto IL_180;
					}
				}
				if ((int)this.selectedSubItem.Tag == 3)
				{
					int num = (int)this.selectedItem.Tag;
					if (num < 0 || num >= this.floorUserList.Count)
					{
						return;
					}
					this.floorUserList[num].szUser = this.selectedSubItem.Text;
				}
				IL_180:
				this.cmbPipeDN.Visible = false;
				this.listViewFloor.Invalidate(true);
			}
		}

		private void cmbLostFocus(object sender, EventArgs e)
		{
			this.cmbPipeDN.Visible = false;
			this.listViewFloor.Invalidate(true);
		}

		private void editBox_LostFocus(object sender, EventArgs e)
		{
			if (this.selectedItem != null && this.selectedSubItem != null && this.bIsAutoChanel)
			{
				bool flag = false;
				double dEquivalent = 0.0;
				try
				{
					dEquivalent = Convert.ToDouble(this.editBox.Text);
					flag = true;
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
				if (!flag)
				{
					return;
				}
				int num = (int)this.selectedItem.Tag;
				if (num < 0 || num >= this.floorUserList.Count)
				{
					return;
				}
				this.selectedSubItem.Text = ((TextBox)sender).Text;
				this.floorUserList[num].dEquivalent = dEquivalent;
			}
			this.editBox.Visible = false;
			this.listViewFloor.Invalidate(true);
		}

		private void InitComb(int nIndex, int nColumn)
		{
			if (nColumn == 3)
			{
				this.cmbPipeDN.Items.Clear();
				using (Dictionary<string, double>.Enumerator enumerator = this.dicName.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						KeyValuePair<string, double> keyValuePair = enumerator.Current;
						this.cmbPipeDN.Items.Add(keyValuePair.Key);
					}
					return;
				}
			}
			if (nColumn == 2)
			{
				if (nIndex < 0 || nIndex >= this.levelList.Count)
				{
					return;
				}
				Level leFrom = this.floorUserList[nIndex].leFrom;
				this.cmbPipeDN.Items.Clear();
				bool flag = false;
				using (List<Level>.Enumerator enumerator2 = this.levelList.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						Level level = enumerator2.Current;
						if (level == leFrom)
						{
							flag = true;
						}
						if (flag)
						{
							this.cmbPipeDN.Items.Add(level.Name);
						}
					}
					return;
				}
			}
			if (nColumn == 1)
			{
				if (nIndex < 0 || nIndex >= this.levelList.Count)
				{
					return;
				}
				Level leTo = this.floorUserList[nIndex].leTo;
				this.cmbPipeDN.Items.Clear();
				foreach (Level level2 in this.levelList)
				{
					this.cmbPipeDN.Items.Add(level2.Name);
					if (level2 == leTo)
					{
						break;
					}
				}
			}
		}

		private void btnCal_Click(object sender, EventArgs e)
		{
			try
			{
				double num = 0.0;
				double num2 = 0.0;
				foreach (object obj in this.listViewFloor.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					string text = listViewItem.SubItems[3].Text;
					double num3 = this.dicName[text];
					double num4 = Convert.ToDouble(listViewItem.SubItems[4].Text);
					num += num3 * num4;
					num2 += num4;
				}
				this.textBox1.Text = Math.Round(num / num2, 1).ToString();
			}
			catch
			{
			}
		}

		public string GetAValue()
		{
			return this.textBox1.Text;
		}

		private Document doc;

		public List<IntegratedBuildUser> floorUserList = new List<IntegratedBuildUser>();

		private List<Level> levelList = new List<Level>();

		private ComboBox cmbPipeDN = new ComboBox();

		private TextBox editBox = new TextBox();

		private ListViewItem selectedItem;

		private ListViewItem.ListViewSubItem selectedSubItem;

		private bool bIsAutoChanel;

		private Dictionary<string, double> dicName = HYCalSQLData.GetSupplyBuildingNameCoefficient();

		private string szUserName = "";
	}
}
